package com.google.code.virtualhockey.vhx.client.srvcom;

import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.user.client.Window;

/**
 * Callback object that allows handling responses of asynchronously dispatched commands.
 *
 * @param <T> the type of the expected response Object
 */
public abstract class CommandCallback<T extends JavaScriptObject> {

  public void onFatal( Command cmd, Throwable t ) {
    // TODO handle fatal failure
  }

  public void onFailure( CommandResponse response ) {
    // TODO handle failure
    Window.alert( "Server error: " + response.getErrorMessage() );
  }

  @SuppressWarnings( "unchecked" )
  public void onSuccess( CommandResponse response ) {
    JavaScriptObject jsobj = response.getResult();
    handle( jsobj != null ? (T) response.getResult().cast() : null );
  }
  
  public abstract void handle( T result );
}
